/**
 * 采购类型下拉控件
 * @author 孙汉斌 2011-12-08
 * 控件说明:显示当前已启用的合同类别
 */
package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class SelectOrderTypeTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String selected;
	private boolean disabled;

	public SelectOrderTypeTag() {
	}

	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String selected = this.selected;
		boolean disabled = this.disabled;

		if (MyFormat.isStrNull(name)) {
			name = "ordertype";
		}
		if (MyFormat.isStrNull(id)) {
			id = name;
		}

		String sql = "";
		StringBuilder html = new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");

		if (disabled) {
			html.append(" disabled");
		}
		html.append(">");
		if (!MyConstant.SELECT_FIRST_OPTION_NO.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option

			if (MyFormat.isStrNull(firstoption))
				firstoption = "全部";

			if (MyFormat.isStrNull(firstvalue))
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			sql = "SELECT ordertypeid,ordertypename FROM OrderType WHERE status='0' ORDER BY ordertypeid ";
			PreparedStatement ps = myJDBC.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			String s_ordertypeid = "";
			String s_ordertypename = "";
			while (rs.next()) {
				s_ordertypeid = rs.getString("ordertypeid");
				s_ordertypename = rs.getString("ordertypename");
				html.append("<option value=\"");
				html.append(s_ordertypeid);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && s_ordertypeid.equals(selected))
					html.append("selected");
				html.append(">");
				html.append(s_ordertypename);
				html.append("</option>");
			}
			rs.close();
			rs = null;
			ps.close();
			ps = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		html.append("</select>");
		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstoption() {
		return firstoption;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public String getFirstvalue() {
		return firstvalue;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

}
